/*
*** (C) 2008 UCMai.com ***

Subj: 公共函数库
Auth: zhouyongtai
Date: 2011-01-03
*/
document.oncontextmenu = function(e){
	e = e || window.event;
	if(window.event) {
		e.cancelBubble = false;
		e.returnValue = false;
	}
	else {
		e.stopPropagation();
		e.preventDefault();
	}
}

window.alert = function(t,msg){
	Ext.MessageBox.show({
	   title: t, msg: msg, icon: Ext.MessageBox.ERROR, buttons: Ext.MessageBox.OK
	});
}

zyt.ajax = function(url, args, callback){
	args = args ? args : {};
	Ext.Ajax.request({
		url: url,
		params: Ext.urlEncode(args),
		method: 'POST',
		callback: function(request, success, response) {
			if (!success) {
				alert(c.error,c.alertMsg.UnknownError);
				return;
			}
			var bError,serverMsg,serverResult;
			if(response.responseText != ''){
				serverMsg = Ext.decode(response.responseText);
				bError = Ext.isDefined(serverMsg.error);
				serverResult = Ext.isDefined(serverMsg.result) ? '' : serverMsg.result;
			}else{
				bError = false;
				serverResult = '';
			}
			if(bError){
				alert(c.error,serverMsg.error);
				return;
			}
			callback(serverResult);
		}
	});
};

zyt.IFNull = function(v1,v2){
	if(Ext.isEmpty(v1)) {
		return v2;
	}
	return v1;
}

var showLoading = function(msg,progressText,el){
	Ext.MessageBox.show({//自定义对话框
		msg:msg,
		progressText:progressText,
		width:300,
		wait:true,
		waitConfig: {interval:200},
		icon: Ext.MessageBox.WARNING,//'ext-mb-save',
		animEl:el,
		title: 'Please Waiting...'
	});
}

var hideLoading = function(){
	Ext.MessageBox.hide();
}

var PermitWindow = function(config) {
	this.config = config;
	this.logon = function() {
		var frm = this.form.getForm();
		if (!frm.isValid()) {
			return;
		}
		showLoading("正在验证，请稍侯...","正在登录中...");
		zyt.ajax(config.url, frm.getValues(), function() {
			//hideLoading();
			top.location = config.redirect;
			//window.location.href = config.redirect;
		}.createDelegate(this));
	};
	this.form = new Ext.FormPanel({
		labelWidth: 60,
		defaults: {width:120, allowBlank:false},
		defaultType: 'textfield',
		border: false,
		bodyStyle: 'padding:10px',
		frame:true,
		keys: {
			key: Ext.EventObject.ENTER,
			fn: this.logon.createDelegate(this)
		},
		items: [{
			fieldLabel: '用户名',
			id:'loginUserName',
			name: 'username',
			maxLength: 50
		}, {
			fieldLabel: '密码',
			name: 'password',
			inputType: 'password',
			maxLength: 30
		}]
	});
	var title = config.title || ' ';
	PermitWindow.superclass.constructor.call(this, {
		title: title + '管理员登录',
		width: 250,
		height: 150,
		resizable: false,
		closable: this.config.closable,
		constrain: true,
		onEsc: Ext.emptyFn,
		layout: 'fit',
		items: this.form,
		iconCls: 'permit',
		modal: this.config.modal,
		buttons: [{
			text: '登录',
			handler: this.logon.createDelegate(this)
		}]
	});

	
	//this.on('beforeshow', function() {
		//Ext.getCmp('mainpanel').body.mask();
	//});
	//this.on('beforehide', function() {
		//Ext.getCmp('mainpanel').body.unmask();
	//});
	
};

//继承类Ext.Window，并添加hide,showLogin方法到PermitWindow的prototype对象中，如果PermitWindow的prototype对象中有同名方法则覆盖。
Ext.extend(PermitWindow, Ext.Window, {
	hide: function() {
		if (this.isVisible()) {
			this.form.destroy();
			PermitWindow.superclass.hide.apply(this, arguments);
		}
	},
	showLogin: function() {
		this.show();
	}
});

Ext.HandleTip = function(){
	var msgCt;
	function createBox(t, s){
		return ['<div class="msg">',
		'<div class="x-box-tl"><div class="x-box-tr"><div class="x-box-tc"></div></div></div>',
		'<div class="x-box-ml"><div class="x-box-mr"><div class="x-box-mc"><h3><span class="fleft">', t, '</span><span class="fright hand" id="closebutton"><img src="/lib/img/tab-close.gif" /></span></h3><div style="clear:both;">', s, '</div></div></div></div>',
		'<div class="x-box-bl"><div class="x-box-br"><div class="x-box-bc"></div></div></div>',
		'</div>'].join('');
	}
	return {
		msg : function(title, format,t){
			if(typeof t=='undefined'){
				t = 3;
			}
			if(!msgCt){
				msgCt = Ext.DomHelper.insertFirst(document.body, {id:'msg-div'}, true);
			}
			msgCt.alignTo(document, 't-t');
			var s = String.format.apply(String, Array.prototype.slice.call(arguments, 1));
			var m = Ext.DomHelper.append(msgCt, {html:createBox(title, s)}, true);
			m.slideIn('t').pause(t).ghost("t", {remove:true});
			Ext.get('closebutton').on('click',function(){
				m.remove();
			});
		}
	};
}();

zyt.Import = function(path,type,title){
	var s,i;
	if(type == "js"){
		var ss = document.getElementsByTagName("script");
		for(i=0;i<ss.length;i++){
			if(ss[i].src && ss[i].src.indexOf(path) != -1)return ss[i];
		}
		s = document.createElement("script");
		s.type = "text/javascript";
		s.src = path;
	}else if(type == "css"){
		var ls = document.getElementsByTagName("link");
		for(i=0;i<ls.length;i++){
			if(ls[i].href && ls[i].href.indexOf(path)!=-1)return ls[i];
		}
		s = document.createElement("link");
		s.rel = "alternate stylesheet";
		s.type = "text/css";
		s.href = path;
		s.title = title;
		s.disabled = false;
	}
	else return;
	var head = document.getElementsByTagName("head")[0];
	head.appendChild(s);
	return s;
};

zyt.setStyle = function(styletitle) {
	var i;
	var links;
	var eflag = false;
	links = document.getElementsByTagName("link");
	
	if(styletitle === "" || styletitle === null){
		for(var i=0;links[i];i++){
			if(links[i].getAttribute("rel").indexOf("style") != -1 && links[i].getAttribute("title")) {
				links[i].disabled = true;
			}
		}
		return;
	}
	for(i=0; links[i]; i++) {
		if(links[i].getAttribute("rel").indexOf("style") != -1 && links[i].getAttribute("title")) {
			links[i].disabled = true;
			if(links[i].getAttribute("title").indexOf(styletitle) != -1)
			{
				links[i].disabled = false;
				eflag=true;
			}
		}
	}
	if(!eflag){
		zyt.Import("/jlib/css/skin/"+styletitle+"/css/css.css","css",styletitle);
		zyt.setStyle(styletitle);
	}
}


zyt.confirmEx = function(act,title,yesCallback) {
	Ext.MessageBox.confirm('提示', '您确定要' + act + '所选择的' + title + '吗?', function(btn) {
		if (btn == 'yes') yesCallback();
	});
}


zyt.ImportMod = function(url,callback){
	/*
	new Ext.LoadMask('show', {
	msg: '正在更新数据，请稍候!',
	removeMask: true //完成后移除
	}).show();*/
	Ext.Ajax.request({
		url: '/jsClass/class/'+url,
		method: 'GET',
		callback: function(request, success, response) {
			zyt.Permit();
			if (!success) {
				alertError('未知错误!<br>请稍候再试.');
				return;
			}

			var headerDom = document.getElementsByTagName('head').item(0);
			if(headerDom.getElementsByTagName("script").length==1){
				headerDom.removeChild(headerDom.getElementsByTagName("script")[0]);
			}

			var jsDom = document.createElement('script');
			jsDom.type = 'text/javascript';
			jsDom.language = 'javascript';
			jsDom.id = url;
			jsDom.defer = "defer";
			jsDom.text = response.responseText;
			headerDom.appendChild(jsDom);
			callback();
		}
	});
};
var alertError = function(msg) {
	Ext.MessageBox.show({
	   title: '错误', msg: msg, icon: Ext.MessageBox.ERROR, buttons: Ext.MessageBox.OK
	});
}
var alertTip = function(msg){
	Ext.MessageBox.show({
	   title: '提示', msg: msg, icon: Ext.MessageBox.WARNING, buttons: Ext.MessageBox.OK
	});
}

zyt.loadMod = function(config){
	var mainpanel=Ext.getCmp('mainpanel');
	/*if(mainpanel != null) {
		if (LABS_NUM == 0)
		{
		mainpanel.items.each(function(item){
			mainpanel.remove(item);
			item.setDisabled(true);
			LABS_NUM--;
		});}
	}*/

	config.id = "tab_" + config.grid;
	Ext.getBody().unmask();
	if (!mainpanel.getItem(config.id)){
		if(mainpanel.items.length>6){
			Ext.MessageBox.alert('系统提醒', '最多只能打开7个窗口，请关闭一些窗口。');
			//delete new config.grid;
			return;
		}else{
			var grid = eval('('+config.grid + ')');
			mainpanel.add({items: new grid(), id: config.id, closable: true ,layout:'fit', border: false});
			mainpanel.getItem(config.id).setTitle(config.title);//config.id
		}
		//LABS_NUM++;
	}

	mainpanel.setActiveTab(mainpanel.getItem(config.id));
	
	if(typeof config.condition==="object"){
		new config.grid().reload(config.condition);
	}
};

zyt.loadPanel = function(config){
	var mainpanel=Ext.getCmp('mainpanel');
	var panel = new Ext.Panel({
		//contentEl:'Configpanel',将写好的HTML放入Panel中
		border:false,
		layout:'fit',
		html:'<div id="Configpanel" marginheight="0" marginwidth="0" width="100%" height="'+ sysCenterHeight +'"></div>'
	});
	if(mainpanel != null) {
		if(typeof(config.depth) == 'undefined') {
			config.depth = 0;
		}
		if (LABS_NUM == 0)
		{
			mainpanel.items.each(function(item){
			mainpanel.remove(item);
			item.setDisabled(true);
			LABS_NUM--;
		});
		}
	
	}
		mainpanel.add(panel);
		mainpanel.getItem(0).setTitle(config.title);
		//Ext.getCmp('mainpanel').doLayout();
		LABS_NUM++;
//	if (!mainpanel.getItem(config.id)){
//		
//	}
	//mainpanel.setActiveTab(mainpanel.getItem(config.id));
}
zyt.loadPage = function(config){
	var mainpanel=Ext.getCmp('mainpanel');
	if(mainpanel!=null) {
		/*if(typeof(config.depth) == 'undefined') {
			config.depth = 0;
		}
		for(var minT = config.depth;LABS_NUM>=minT;LABS_NUM--){
			var item = mainpanel.getItem(LABS_NUM);
			if(item){
				mainpanel.remove(item);
				item.setDisabled(true);
			}
		}*/
	}
	config.id = "tab_" + config.url;
	if (!mainpanel.getItem(config.id)){
		if(mainpanel.items.length>6){
			Ext.getBody().unmask();
			Ext.MessageBox.alert('系统提醒', '最多只能打开7个窗口，请关闭一些窗口。');
			//delete new config.grid;
			return;
		}else{
			var oMainPanel = new Ext.Panel({
				//id:'page' + config.depth,
				border:false,
				layout:'fit',
				html:'<iframe src="'+ config.url +'"  marginheight="0" marginwidth="0" scrolling="auto" frameborder="0" width="100%" height="'+ sysCenterHeight +'"></iframe>'
			});
			mainpanel.add({items: oMainPanel, id: config.id, closable: true ,layout:'fit', border: false});
			mainpanel.getItem(config.id).setTitle(config.text);
		}
	}
	mainpanel.setActiveTab(mainpanel.getItem(config.id));
	//LABS_NUM++;
	Ext.getBody().unmask();
};
function now(){
	return +new Date;
}
function var_dump(obj) {
	var type = Object.prototype.toString.call(obj);
	var str = '';
	if (type === "[object Number]") {
		str += "Number(" + obj + ")";
	} else if (type === "[object Boolean]") {
		str += "Boolean(" + obj + ")";
	} else if (type === "[object String]") {
		str += "String("+obj.length+")\""+obj+"\"";
	} else if (type === "[object Array]") {
		var len = obj.length;
		str += "Array("
		for (var i=0;i < len ;i++ )
		{
			var comma = (len == (i+1)) ? "" : ", ";
			str += i + " => " + obj[i];
			//str += i + " => " + var_dump(obj[i]) + comma;
		}
		str += ")";
	}else {
		
		str += "Object{";
		for(var o in obj){
			var comma = ", ";
			//str += o + ": " + var_dump(obj[o]) + comma;
			str += o + ": " + obj[o];
		}
		str += "}";
	}
	return str;
}
function nodeAction(obj){
	Ext.getBody().mask('loading', 'x-mask-loading');
	if(obj.mode == 1) {
		zyt.ImportMod(obj.url ,function(){zyt.loadMod({title: obj.text, grid: obj.grid});});
	}else{
		zyt.loadPage(obj);
	}
	//loadPanel
}
function MachakFull(Ie,other){
	x=screen.availWidth;
	y=screen.availHeight;
	target = parseFloat(navigator.appVersion.substring(navigator.appVersion.indexOf('.')-1,navigator.appVersion.length));
	if((navigator.appVersion.indexOf("Mac")!=-1) &&(navigator.userAgent.indexOf("MSIE")!=-1) &&(parseInt(navigator.appVersion)==4))
	window.open(other,"sub",'scrollbars=yes');
	if (target >= 4){
	 if (navigator.appName=="Netscape"){
		var MachakFull=window.open(other,"MachakFull",'scrollbars=yes','width='+x+',height='+y+',top=0,left=0');
	 MachakFull.moveTo(0,0);
	 MachakFull.resizeTo(x,y);}
	if (navigator.appName=="Microsoft Internet Explorer")
	 window.open(Ie,"MachakFull","fullscreen=yes");
	 }
	 else window.open(other,"sub",'scrollbars=yes');
}

function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

var aGender = [[0,'男'],[1,'女'],[2,"未知"]];
function genderStore(){
	//用以填充性别下拉框
	var genderStore = new Ext.data.SimpleStore({
		fields:[{name:'GenderTID',type:'int'},
			{name:'GenderT'}]
	});
	genderStore.loadData(aGender);
	return genderStore;
}